﻿using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using HRM.Model;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using HRM.View;
using GalaSoft.MvvmLight.Messaging;
using HRM.Helper;
using System.Windows;

namespace HRM.ViewModel
{
    public class GroupUserViewModel : ViewModelGeneric<NhomNguoiDung>
    {
        private List<ChucNang> _functions = new List<ChucNang>();
        private IAuthenticationService _authenticationService;
        private IEntityService<NguoiSuDung> _userService;

        public GroupUserViewModel(IEntityService<NhomNguoiDung> service, IAuthenticationService authenticationService, IEntityService<NguoiSuDung> userService)
            : base(service)
        {
            if (SimpleIoc.Default.IsRegistered<List<ChucNang>>()) _functions = SimpleIoc.Default.GetInstance<List<ChucNang>>();
            if (ListEntities.Count > 0) SelectedItem = ListEntities[0];
            _authenticationService = authenticationService;
            _userService = userService;
        }

        public override NhomNguoiDung SelectedItem
        {
            get
            {
                return base.SelectedItem;
            }
            set
            {
                if (_selectedItem != value)
                {
                    _selectedItem = value;

                    if (value != null)
                    {
                        foreach (var item in _functions)
                        {
                            item.IsChecked = false;
                            foreach (var f in _selectedItem.PhanQuyens)
                            {
                                if (f.MaChucNang == item.MaChucNang)
                                {
                                    item.IsChecked = true;
                                    break;
                                }
                            }
                        }
                    }

                    RaisePropertyChanged("SelectedItem");
                }
            }
        }

        private NguoiSuDung _selectedUser;

        public NguoiSuDung SelectedUser
        {
            get { return _selectedUser; }
            set
            {
                if (_selectedUser != value)
                {
                    _selectedUser = value;
                    RaisePropertyChanged("User");
                }
            }
        }

        #region Commands

        public RelayCommand<int> ChangingCommand
        {
            get
            {
                return new RelayCommand<int>((obj) =>
                {
                    var cn = _functions.Where(x => x.MaChucNang == obj).FirstOrDefault();
                    if (cn.IsChecked)
                    {
                        if (_authenticationService.Add(SelectedItem, cn))
                        {
                            SelectedItem.PhanQuyens.Add(new PhanQuyen()
                            {
                                MaChucNang = obj
                            });
                            RaisePropertyChanged("ListEntities");
                        }
                    }
                    else
                    {
                        if (_authenticationService.Delete(SelectedItem, cn))
                        {
                            var pq = SelectedItem.PhanQuyens.Where(x => x.MaChucNang == obj).FirstOrDefault();
                            SelectedItem.PhanQuyens.Remove(pq);
                            RaisePropertyChanged("ListEntities");
                        }
                    }
                }, (obj) => _user.GetPermisstion);
            }
        }

        public RelayCommand AddCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                }, () => _user.GetPermisstion);
            }
        }

        public RelayCommand ForwardCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    if (SelectedItem != null)
                    {
                        if (SelectedUser != null)
                        {
                            SimpleIoc.Default.Register<NhomNguoiDung>(() => SelectedItem, "CurrentGroup");
                            SimpleIoc.Default.Register<NguoiSuDung>(() => SelectedUser, "User");
                            if (!SimpleIoc.Default.IsRegistered<List<NhomNguoiDung>>())
                            {
                                SimpleIoc.Default.Register<List<NhomNguoiDung>>(() => ListEntities);
                            }
                            ChooseGroupUserDialog dialog = new ChooseGroupUserDialog();
                            dialog.ShowDialog();
                        }
                    }
                    else
                    {
                        Messenger.Default.Send(new StatusMessage("Phải chọn nhóm trước", 3000, StatusType.Warning));
                    }
                }, () => _user.GetPermisstion);
            }
        }

        public RelayCommand RemoveCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    if (MessageBox.Show("Người dùng sẽ được xóa khỏi hệ thống \n Bạn có chắc chắn không?", "Xác nhận", MessageBoxButton.OKCancel, MessageBoxImage.Warning, MessageBoxResult.Cancel) == MessageBoxResult.OK)
                    {
                        _userService.Delete(SelectedUser);
                    }
                });
            }
        }

        public override void Refresh()
        {
            if(ListEntities != null) ListEntities.Clear();
            ListEntities = _service.GetAll();
        }

        #endregion
    }
}
